package com.totobox.apps.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;

/**
 * Various bitmap utilities
 * 
 * @author alessandro
 * 
 */
public class BitmapUtils {

	private static final Paint sPaint = new Paint();
	private static final Rect sBounds = new Rect();
	private static final Rect sOldBounds = new Rect();
	private static Canvas sCanvas = new Canvas();
	
	/**
	 * Resize a bitmap
	 * 
	 * @param input
	 * @param destWidth
	 * @param destHeight
	 * @return
	 * @throws OutOfMemoryError
	 */
	public static Bitmap resizeBitmap( final Bitmap input, int destWidth, int destHeight ) throws OutOfMemoryError {
		return resizeBitmap( input, destWidth, destHeight, 0 );
	}

	/**
	 * Resize a bitmap object to fit the passed width and height
	 * 
	 * @param input
	 *           The bitmap to be resized
	 * @param destWidth
	 *           Desired maximum width of the result bitmap
	 * @param destHeight
	 *           Desired maximum height of the result bitmap
	 * @return A new resized bitmap
	 * @throws OutOfMemoryError
	 *            if the operation exceeds the available vm memory
	 */
	public static Bitmap resizeBitmap( final Bitmap input, int destWidth, int destHeight, int rotation ) throws OutOfMemoryError {

		int dstWidth = destWidth;
		int dstHeight = destHeight;
		final int srcWidth = input.getWidth();
		final int srcHeight = input.getHeight();

		if ( rotation == 90 || rotation == 270 ) {
			dstWidth = destHeight;
			dstHeight = destWidth;
		}

		boolean needsResize = false;
		float p;
		if ( ( srcWidth > dstWidth ) || ( srcHeight > dstHeight ) ) {
			needsResize = true;
			if ( ( srcWidth > srcHeight ) && ( srcWidth > dstWidth ) ) {
				p = (float) dstWidth / (float) srcWidth;
				dstHeight = (int) ( srcHeight * p );
			} else {
				p = (float) dstHeight / (float) srcHeight;
				dstWidth = (int) ( srcWidth * p );
			}
		} else {
			dstWidth = srcWidth;
			dstHeight = srcHeight;
		}

		if ( needsResize || rotation != 0 ) {
			Bitmap output;

			if ( rotation == 0 ) {
				output = Bitmap.createScaledBitmap( input, dstWidth, dstHeight, true );
			} else {
				Matrix matrix = new Matrix();
				matrix.postScale( (float) dstWidth / srcWidth, (float) dstHeight / srcHeight );
				matrix.postRotate( rotation );
				output = Bitmap.createBitmap( input, 0, 0, srcWidth, srcHeight, matrix, true );
			}
			return output;
		} else
			return input;
	}
	
	/**
	 * 원본 이미지가 지정된 크기 안에 중앙에 배치된 새로운 이미지를 생성한다.
	 * 
	 * @param bitmap
	 *            원본 이미지
	 * @param width
	 *            너비
	 * @param height
	 *            높이
	 * @param color
	 *            배경 색상
	 * @param context
	 *            The application's context.
	 * @return 생성된 새로운 이미지
	 */
	static Bitmap centerToFit(Bitmap bitmap, int width, int height, int color,Context context) {
		final int bitmapWidth = bitmap.getWidth();
		final int bitmapHeight = bitmap.getHeight();

		if (bitmapWidth < width || bitmapHeight < height) {
			Bitmap centered = Bitmap.createBitmap(bitmapWidth < width ? width
					: bitmapWidth, bitmapHeight < height ? height
					: bitmapHeight, Bitmap.Config.RGB_565);

			Canvas canvas = new Canvas(centered);
			canvas.drawColor(color);
			canvas.drawBitmap(bitmap, (width - bitmapWidth) / 2.0f,
					(height - bitmapHeight) / 2.0f, null);

			bitmap = centered;
		}

		return bitmap;
	}

	/**
	 * thumbnail 이미지를 만들어 낸다.
	 * 
	 * @param bitmap
	 *            원본 이미지
	 * @param nIconWidth
	 *            thumbnail 너비
	 * @param nIconHeight
	 *            thumbnail 높이
	 * @param context
	 *            The application's context.
	 * 
	 * @return thumbnail 이미지
	 */
	public static Bitmap createBitmapThumbnail(Bitmap bitmap, int nIconWidth,int nIconHeight) {
		if (null == bitmap || 0 >= nIconWidth || 0 >= nIconHeight)
			return null;

		final int bitmapWidth = bitmap.getWidth();
		final int bitmapHeight = bitmap.getHeight();

		if (nIconWidth > 0 && nIconHeight > 0 && (nIconWidth < bitmapWidth || nIconHeight < bitmapHeight)) {
			final float ratio = (float) bitmapWidth / bitmapHeight;

			if (bitmapWidth > bitmapHeight) {
				nIconHeight = (int) (nIconWidth / ratio);
			} else if (bitmapHeight > bitmapWidth) {
				nIconWidth = (int) (nIconHeight * ratio);
			}

			final Bitmap.Config c = Bitmap.Config.RGB_565;
			final Bitmap bmpThumb = Bitmap.createBitmap(nIconWidth,nIconHeight, c);
			final Canvas canvas = sCanvas;
			final Paint paint = sPaint;

			canvas.setBitmap(bmpThumb);
			paint.setDither(false);
			paint.setFilterBitmap(true);
			sBounds.set((nIconWidth - nIconWidth) / 2,(nIconHeight - nIconHeight) / 2, nIconWidth, nIconHeight);
			sOldBounds.set(0, 0, bitmapWidth, bitmapHeight);
			canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint);
			return bmpThumb;
		}

		return bitmap;
	}
	

}
